package com.mobartisan.vehiclenetstore.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.igexin.sdk.PushManager;
import com.mobartisan.vehiclenetstore.ECarApplication;
import com.mobartisan.vehiclenetstore.R;
import com.mobartisan.vehiclenetstore.constants.Constants;
import com.mobartisan.vehiclenetstore.getui_push.IntentPushRemindService;
import com.mobartisan.vehiclenetstore.getui_push.PushRemindService;
import com.mobartisan.vehiclenetstore.permissions.PermissionContacts;
import com.mobartisan.vehiclenetstore.permissions.PermissionsActivity;
import com.mobartisan.vehiclenetstore.permissions.PermissionsChecker;
import com.mobartisan.vehiclenetstore.service.InternalExchangeService;
import com.mobartisan.vehiclenetstore.ui.base.BaseActivity;
import com.mobartisan.vehiclenetstore.util.ObjectUtils;
import com.mobartisan.vehiclenetstore.util.SharedPreferencesUtil;

import java.io.File;

import static android.R.attr.password;
import static com.igexin.push.core.g.v;


public class WelcomeActivity extends BaseActivity {

    private static final String TAG = WelcomeActivity.class.getSimpleName();

    private static final int REQUEST_PERMISSION = 0;

    private String onOffPush = "";

    private PackageInfo info = null;

    String versionName = "";

    private static final int REQUEST_CODE = 0; // 请求码

    public static final String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS
    };
    private PermissionsChecker mPermissionsChecker; // 权限检测器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //解决APP按home键时，再次点击桌面程序图标，程序重新启动的问题.
        if (!isTaskRoot()) {
            finish();
            return;
        }
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        window.setAttributes(params);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_welcome);
        mPermissionsChecker = new PermissionsChecker(context);
        // 缺少权限时, 进入权限配置页面
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mPermissionsChecker.lacksPermissions(PermissionContacts.PERMISSIONS)) {
                    PermissionsActivity.startActivityForResult(context, REQUEST_CODE, PermissionContacts.PERMISSIONS);
                } else if (isFirst()) {
                    Intent intent = new Intent(context, GuidanceActivity.class);
                    InternalExchangeService.registerExchangeParam(intent, true);
                    InternalExchangeService.startActivity(context, intent);
                    finish();
                } else {
                    Intent intent = new Intent(context, MainActivity.class);
                    InternalExchangeService.registerExchangeParam(intent, true);
                    InternalExchangeService.startActivity(context, intent);
                    finish();
                }
                PushManager.getInstance().initialize(ECarApplication.getInstance(), PushRemindService.class);
            }
        }, 2000);

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), IntentPushRemindService.class);
        // 应用未启动, 个推 service已经被唤醒,显示该时间段内离线消息

        onOffPush = SharedPreferencesUtil.getInstance().getStringValue(Constants.On_Off_Push);
        if (!ObjectUtils.isStringEmpty(onOffPush)) {
            if (onOffPush.equals("onpush")) {
                SharedPreferencesUtil.getInstance().setStringValue(Constants.On_Off_Push,"onpush");
                PushManager.getInstance().turnOnPush(context);
            } else if (onOffPush.equals("offpush")) {
                SharedPreferencesUtil.getInstance().setStringValue(Constants.On_Off_Push,"offpush");
                PushManager.getInstance().turnOffPush(context);
            }

        }
        SharedPreferencesUtil.getInstance().setStringValue(Constants.On_Off_Push,"onpush");
    }
    private boolean isFirst() {
        versionName = SharedPreferencesUtil.getInstance().getStringValue("versionName");
        try {
            info = this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (ObjectUtils.isStringEmpty(versionName)) {
            SharedPreferencesUtil.getInstance().setStringValue("versionName", info.versionName);
            Log.d("", "info.versionName:" + info.versionName);
            return true;
        }else if (!versionName.equals(info.versionName)){
            SharedPreferencesUtil.getInstance().setStringValue("versionName", info.versionName);
            return true;
        }
        return false;
    }


    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_PERMISSION);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            finish();
        } else if ((requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_GRANTED)) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isFirst()) {
//                    if (ObjectUtils.isStringNotEmpty(username) && ObjectUtils.isStringNotEmpty(password)) {
                        Intent intent = new Intent(WelcomeActivity.this, GuidanceActivity.class);
                        InternalExchangeService.registerExchangeParam(intent, true);
                        InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        InternalExchangeService.registerExchangeParam(intent, true);
                        InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                        finish();
                    }
                    PushManager.getInstance().registerPushIntentService(ECarApplication.getInstance(), IntentPushRemindService.class);
                }

            }, 2000);

        } else
            finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade, R.anim.hold);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public Activity getActivity() {
        return this;
    }


}
